package binarysearch;

/**
 * Created by JiaJia on 2019/5/28 0028.
 * Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
 * Output: 2
 *
 */
public class SingleInArray {
    public static int singleInArray(int[] nums){
        int l=0,h=nums.length-1;
        while (l<h){
            int mid=l+(h-l)/2;
            if (mid%2==1){
                mid--;
            }
            if (nums[mid]==nums[mid+1]){
                l=mid+2;
            }else {
                h=mid;
            }
        }
        return nums[l];
    }

    public static void main(String[] args) {
        int[] num1={1,1,2,3,3,4,4,8,8};
        int[] num2={1,1,2,2,3,3,4,8,8};
        System.out.println(singleInArray(num1));
        System.out.println(singleInArray(num2));
    }
}